home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-09-09 | 16.0 KB | 606 lines | [TEXT/CWIE] |
- // Simple framework for Macintosh sample code
- //
- // David Hayward and Nick Thompson
- // Developer Technical Support
- // AppleLink: DEVSUPPORT
- //
- // Copyrite 1995, Apple Computer,Inc
- //
- // Access routines for the winHandle structure that is used this
- // simple application framework
- //
- // 9/2/94 nick first cut
- // 10/4/94 nick add field for the doc FSSpec
- // 11/16/94 david in the DocumentRecord structure
- // added fields dPicHandle, dPictPalette, dPictCTab
- // changed field dDocFsSp to dFSSpec
- // changed field dPageFormat to dGXFormat
- // changed field dDocumentJob to dGXJob
- // added several getters/setters routines
- // changed some code to use the new getters/setters
- // changed the #define SetWinumentHandleForWindowRefcon to a function SetWindowDocumentHandle
- // changed the #define GetWinumentHandleForWindowRefcon to a function GetWindowDocumentHandle
- // changed varaible names from "theDoc" to "doc" for brevity
- // added some lines to DisposeDocumentHandle so doc's palette and color table are disposed
- // 3/10/95 david fixed bugs in FindWinHandle
- // 8/23/95 david strengthened sanity checks in Get/SetWindowWinHandle
- // changed to use accessors in <Windows.h> v2.1 under STRICT_WINDOWS
- // 2/15/96 david improved FindWinHandle to also seach subtype
-
- #define OLDROUTINENAMES 1
-
- #include <Windows.h>
- #include <Files.h>
- #include <TextUtils.h>
-
- #include "appErrors.h"
-
- #include "win.h"
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE DEFINES
- |**| ==============================================================================
- \**/
- #define kWindowKind 123
- //#define GetWindowKind( w ) (((WindowPeek)(w))->windowKind)
- //#define SetWindowKind( w, k ) (((WindowPeek)(w))->windowKind=(k))
- //#define GetNextWindow( w ) ((WindowRef)(((WindowPeek)(w))->nextWindow))
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTION PROTOTYPES
- |**| ==============================================================================
- \**/
- Boolean SameFSSpec ( FSSpec *fs1, FSSpec *fs2 ) ;
-
-
- /**\
- |**| ==============================================================================
- |**| PUBLIC FUNCTIONS
- |**| ==============================================================================
- \**/
-
- /*------------------------------------------------------------------------------*\
- GetWindowWinHandle
- *------------------------------------------------------------------------------*
- This routine gets the winHandle from the refcon field of a window
- and returns it.
- \*------------------------------------------------------------------------------*/
- winHandle GetWindowWinHandle ( WindowRef w )
- {
- winHandle win;
- short kind;
-
- if (w==nil)
- return nil ;
-
- kind = GetWindowKind( w ) ;
- if (kind != kWindowKind) // validate - check for our windowKind
- return nil ;
-
- win = (winHandle) GetWRefCon( w ) ;
- if (win == nil) // validate - check for nil
- return nil ;
-
- // validate - check for the document creator
- // as the first long of the struct
- if( GetWinCreator(win) != kDocumentCreator )
- win = nil;
-
- return win ; // assert: this will either be valid or nil
- }
-
-
- /*------------------------------------------------------------------------------*\
- GetWindowWinHandle
- *------------------------------------------------------------------------------*
- This routine puts the winHandle into the refcon field of a window.
- \*------------------------------------------------------------------------------*/
- void SetWindowWinHandle ( WindowRef w, winHandle win )
- {
- SetWRefCon( w, (long)win ) ;
- SetWindowKind( w, kWindowKind ) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- FrontWin/GetNextWin
- *------------------------------------------------------------------------------*
- This routinee are equivalent to FindWindow and GetNextWindow.
- \*------------------------------------------------------------------------------*/
- winHandle GetFrontWindowWinHandle( void )
- {
- return GetWindowWinHandle( FrontWindow() ) ;
- }
-
- winHandle FrontWin( void )
- {
- winHandle win = nil;
- WindowRef window ;
-
- window = FrontWindow() ;
- while ( window != nil && win==nil ) {
- win = GetWindowWinHandle( window ) ;
- window = GetNextWindow( window ) ;
- }
- return win;
- }
-
- winHandle GetNextWin( winHandle win )
- {
- winHandle next = nil;
- WindowRef window ;
-
- window = GetWinWindow(win) ;
- while ( window != nil && next==nil ) {
- window = GetNextWindow( window ) ;
- next = GetWindowWinHandle( window ) ;
- }
- return next;
- }
-
- /*------------------------------------------------------------------------------*\
- FindWinHandle
- *------------------------------------------------------------------------------*
- This routine searches through the active window list to locate those that
- have winHandle's with a given FSSpec, type or subtype. It can be used either
- to count the number of matches or to return the matches in the result buffer.
- The logic is basically the following:
- if result buffer is nil
- return num of winHandles that match the specPtr, type or subtype parameters
- the index and count fields are ignored
- else
- copy into result buffer up to count winHandles
- that match the specPtr and/or type parameters
- starting with the index-th match
- return the the number of winHandles copied into the buffer
- \*------------------------------------------------------------------------------*/
- short FindWinHandle ( FSSpec *specPtr,
- OSType type,
- OSType subtype,
- short index,
- short count,
- winHandle *result )
- {
- WindowRef w;
- FSSpec docSpec ;
- winHandle win = nil;
- short n = 0;
- short stopCount, foundCount;
- Boolean sameSpec, sameType, sameSubtype;
-
- stopCount = count ;
- foundCount = 0 ;
-
- w = FrontWindow() ;
- if (w==nil) return 0 ; // no window are open
-
- do {
- win = GetWindowWinHandle( w ) ;
- if ( win != nil )
- {
- sameSpec = true;
- sameType = true;
- sameSubtype = true;
-
- if (specPtr) docSpec = GetWinFSSpec( win ) ;
- if (specPtr) sameSpec = SameFSSpec( &docSpec, specPtr) ;
- if (type) sameType = (type==GetWinType(win)) ;
- if (subtype) sameSubtype = (subtype==GetWinSubtype(win)) ;
-
- if ( sameSpec && sameType && sameSubtype )
- {
- if ( result==nil )
- foundCount++ ;
- else
- {
- n++;
- if (n>=index)
- {
- foundCount++ ;
- result[foundCount-1] = win;
- }
- }
- }
- }
- w = GetNextWindow( w ) ;
-
- } while ( (w!=nil) && ((foundCount<stopCount) || (result==nil)) ) ;
-
- return foundCount;
- }
-
-
- /*------------------------------------------------------------------------------*\
- NewWinHandle
- *------------------------------------------------------------------------------*
- This routine allocates a new winRecord, sets record's AllocProc, and
- then calls that AllocProc to do the rest of the work.
- \*------------------------------------------------------------------------------*/
- OSErr NewWinHandle ( winHandle *win, AllocProcPtr allocProc )
- {
- OSErr err = noErr ;
-
- *win = (winHandle)NewHandleClear( sizeof(winRecord) ) ;
- if( *win == nil )
- {
- err = MemError() ;
- if (err==noErr) err=memFullErr ; // should only need this when memerror is noErr
- return err ;
- }
-
- // init the creator field
- SetWinCreator( *win, kDocumentCreator ) ;
-
- // allocate any custom data for the win
- SetWinAllocProc( *win, allocProc ) ;
- err = CallWinAllocProc( *win ) ;
- if ( err != noErr)
- DisposeHandle( (Handle)(*win) ) ;
-
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- NewWinHandle
- *------------------------------------------------------------------------------*
- This routine calls the winHandle's DisposeProc, disposes of the structures
- associated with the winHandle, and lastly, disposed of the winHandle
- itself.
- \*------------------------------------------------------------------------------*/
- OSErr DisposeWinHandle ( winHandle win )
- {
- OSErr err = noErr ;
-
- // deallocate the fields…
-
- // dispose of any custom data for the win
- CallWinDisposeProc( win ) ;
-
- // dispose of the window we used
- if ( GetWinWindow(win) != nil )
- {
- DisposeWindow( GetWinWindow(win) ) ;
- err = QDError() ;
- }
-
- #ifdef PIGS_SHELL_PRINT
- // get rid of the memory allocated for the print record
- if ( GetWinPrintRec(win) != nil )
- {
- DisposeHandle((Handle)GetWinPrintRec(win)) ;
- err = MemError() ;
- }
- #endif
-
- // get rid of the memory for the document handle - DO THIS LAST
- if ( err == noErr )
- {
- DisposeHandle( (Handle)win ) ;
- err = MemError() ;
- }
-
- return err ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- GetWinXxxx / SetWinXxxx
- *------------------------------------------------------------------------------*
- These routines get/set the data fields of a winHandle.
- \*------------------------------------------------------------------------------*/
-
-
- OSType GetWinCreator ( winHandle win )
- { return (win) ? ((**win).Creator) : (nil) ;
- }
- void SetWinCreator ( winHandle win, OSType creator )
- { if (win) (**win).Creator = creator ;
- }
-
-
- OSType GetWinType ( winHandle win )
- { return (win) ? ((**win).Type) : (nil) ;
- }
- void SetWinType ( winHandle win, OSType type )
- { if (win) (**win).Type = type ;
- }
-
-
- OSType GetWinSubtype ( winHandle win )
- { return (win) ? ((**win).Subtype) : (nil) ;
- }
- void SetWinSubtype ( winHandle win, OSType type )
- { if (win) (**win).Subtype = type ;
- }
-
-
- FSSpec GetWinFSSpec ( winHandle win )
- { return (**win).FSSpec ;
- }
- void SetWinFSSpec ( winHandle win, FSSpec *theFSSpec )
- { if (win) (**win).FSSpec = *theFSSpec ;
- }
-
-
- WindowRef GetWinWindow ( winHandle win )
- { return (win) ? ((**win).Window) : (nil) ;
- }
- void SetWinWindow ( winHandle win, WindowRef theWindow )
- { if (win) (**win).Window = theWindow ;
- }
-
-
- Rect GetWinRect ( winHandle win )
- { return (**win).Rect ;
- }
- void SetWinRect ( winHandle win, Rect theRect )
- { if (win) (**win).Rect = theRect ;
- }
-
-
- Rect GetWinSizeRect ( winHandle win )
- { return (**win).SizeRect ;
- }
- void SetWinSizeRect ( winHandle win, Rect theRect )
- { if (win) (**win).SizeRect = theRect ;
- }
-
-
- Boolean GetWinDirty ( winHandle win )
- { return (win) ? ((**win).Dirty) : (nil) ;
- }
- void SetWinDirty ( winHandle win, Boolean dirty )
- { if (win) (**win).Dirty = dirty ;
- }
-
-
- #ifdef PIGS_SHELL_PRINT
-
- THPrint GetWinPrintRec ( winHandle win )
- { return (win) ? ((**win).PrintRec) : (nil) ;
- }
- void SetWinPrintRec ( winHandle win, THPrint thePrintRec )
- { if (win) (**win).PrintRec = thePrintRec ;
- }
-
- #endif
-
-
- Handle GetWinData ( winHandle win )
- { return (win) ? ((**win).Data) : (nil) ;
- }
- void SetWinData ( winHandle win, Handle h )
- { if (win) (**win).Data = h ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- CallWinXxxx / SetWinXxxx
- *------------------------------------------------------------------------------*
- These routines call/set the procPtrs of a winHandle.
- \*------------------------------------------------------------------------------*/
-
-
- void CallWinActivateProc (winHandle win, Boolean activating)
- { if ( (win) && ((**win).ActivateProc) )
- ((**win).ActivateProc)(win, activating) ;
- }
- void SetWinActivateProc (winHandle win, ActivateProcPtr f)
- { if (win) (**win).ActivateProc = f;
- }
-
-
- void CallWinResumeProc (winHandle win, Boolean resuming)
- { if ( (win) && ((**win).ResumeProc) )
- ((**win).ResumeProc)(win, resuming) ;
- }
- void SetWinResumeProc (winHandle win, ResumeProcPtr f)
- { if (win) (**win).ResumeProc = f;
- }
-
-
- void CallWinUpdateProc (winHandle win, EventRecord *e)
- { if ( (win) && ((**win).UpdateProc) )
- ((**win).UpdateProc)(win, e) ;
- }
- void SetWinUpdateProc (winHandle win, UpdateProcPtr f)
- { if (win) (**win).UpdateProc = f;
- }
-
-
- void CallWinClickProc (winHandle win, EventRecord *e)
- { if ( (win) && ((**win).ClickProc) )
- ((**win).ClickProc)(win, e) ;
- }
- void SetWinClickProc (winHandle win, ClickProcPtr f)
- { if (win) (**win).ClickProc = f;
- }
-
-
- void CallWinKeyProc (winHandle win, EventRecord *e)
- { if ( (win) && ((**win).KeyProc) )
- ((**win).KeyProc)(win, e) ;
- }
- void SetWinKeyProc (winHandle win, KeyProcPtr f)
- { if (win) (**win).KeyProc = f;
- }
-
-
- void CallWinNullProc (winHandle win, EventRecord *e)
- { if ( (win) && ((**win).NullProc) )
- ((**win).NullProc)(win, e) ;
- }
- void SetWinNullProc (winHandle win, NullProcPtr f)
- { if (win) (**win).NullProc = f;
- }
-
-
- void CallWinMenuProc (winHandle win, long m, Boolean *didit)
- { if ( (win) && ((**win).MenuProc) )
- ((**win).MenuProc)(win, m, didit) ;
- }
- void SetWinMenuProc (winHandle win, MenuProcPtr f)
- { if (win) (**win).MenuProc = f;
- }
-
-
- void CallWinUpdateMenusProc (winHandle win)
- { if ( (win) && ((**win).UpdateMenusProc) )
- ((**win).UpdateMenusProc)(win) ;
- }
- void SetWinUpdateMenusProc (winHandle win, UpdateMenusProcPtr f)
- { if (win) (**win).UpdateMenusProc = f;
- }
-
-
- void CallWinResizeProc (winHandle win)
- { if ( (win) && ((**win).ResizeProc) )
- ((**win).ResizeProc)(win) ;
- }
- void SetWinResizeProc (winHandle win, ResizeProcPtr f)
- { if (win) (**win).ResizeProc = f;
- }
-
-
- OSErr CallWinAllocProc (winHandle win )
- { if ( (win) && ((**win).AllocProc) )
- return ((**win).AllocProc)(win) ;
- else return eFatalNoProcPtr ;
- }
- void SetWinAllocProc (winHandle win, AllocProcPtr f)
- { if (win) (**win).AllocProc = f;
- }
-
-
- OSErr CallWinNewProc (winHandle win )
- { if ( (win) && ((**win).NewProc) )
- return ((**win).NewProc)(win) ;
- else return eFatalNoProcPtr ;
- }
- void SetWinNewProc (winHandle win, NewProcPtr f)
- { if (win) (**win).NewProc = f;
- }
-
-
- OSErr CallWinOpenProc (winHandle win )
- { if ( (win) && ((**win).OpenProc) )
- return((**win).OpenProc)(win) ;
- else return eFatalNoProcPtr ;
- }
- void SetWinOpenProc (winHandle win, OpenProcPtr f)
- { if (win) (**win).OpenProc = f;
- }
-
-
- void CallWinCloseProc (winHandle win)
- { if ( (win) && ((**win).CloseProc) )
- ((**win).CloseProc)(win) ;
- }
- void SetWinCloseProc (winHandle win, CloseProcPtr f)
- { if (win) (**win).CloseProc = f;
- }
-
-
- void CallWinDisposeProc ( winHandle win )
- { if ( (win) && ((**win).DisposeProc) )
- ((**win).DisposeProc)(win) ;
- }
- void SetWinDisposeProc ( winHandle win, DisposeProcPtr f )
- { if (win) (**win).DisposeProc = f;
- }
-
-
- #ifdef PIGS_SHELL_PRINT
-
- OSErr CallWinPageCountProc (winHandle win, short *pageCount)
- { if ( (win) && ((**win).PageCountProc) )
- return ((**win).PageCountProc)(win,pageCount) ;
- else return eFatalNoProcPtr ;
- }
- void SetWinPageCountProc (winHandle win, PageCountProcPtr f)
- { if (win) (**win).PageCountProc = f;
- }
-
-
- OSErr CallWinPagePrintProc (winHandle win, GrafPtr imagingPort, short pageNum)
- { if ( (win) && ((**win).PagePrintProc) )
- return ((**win).PagePrintProc)(win,imagingPort,pageNum) ;
- else return eFatalNoProcPtr ;
- }
- void SetWinPagePrintProc (winHandle win, PagePrintProcPtr f)
- { if (win) (**win).PagePrintProc = f;
- }
-
- #endif
-
-
-
- OSErr CallAllWinNullProcs( EventRecord *event )
- {
- winHandle win ;
- win = FrontWin() ;
- while ( win ) {
- CallWinNullProc( win, event ) ;
- win = GetNextWin(win) ;
- }
- return noErr;
- }
-
- OSErr CallAllWinResumeProcs( Boolean resuming )
- {
- winHandle win ;
- win = FrontWin() ;
- while ( win ) {
- CallWinResumeProc( win, resuming ) ;
- win = GetNextWin(win) ;
- }
- return noErr;
- }
-
- OSErr CallAllWinUpdateMenusProcs( void )
- {
- winHandle win ;
- win = FrontWin() ;
- while ( win ) {
- CallWinUpdateMenusProc( win ) ;
- win = GetNextWin(win) ;
- }
- return noErr;
- }
-
- OSErr CallAllWinMenuProcs( long m, Boolean *didit )
- {
- winHandle win ;
- win = FrontWin() ;
- while ( win && !(*didit) ) {
- CallWinMenuProc( win, m, didit ) ;
- if (!(*didit)) win = GetNextWin(win) ;
- }
- return noErr;
- }
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- SameFSSpec
- *------------------------------------------------------------------------------*
- This routine checks to FSSpec records to see if they are the same.
- \*------------------------------------------------------------------------------*/
- static Boolean SameFSSpec ( FSSpec *fs1, FSSpec *fs2 )
- {
- return ( (fs1->vRefNum == fs2->vRefNum) &&
- (fs1->parID == fs2->parID) &&
- (EqualString(fs1->name,fs2->name, false, true)) ) ;
- }
-